//
//  EditViewController.m
//  dddddemo
//
//  Created by lanou3g on 15/11/4.
//  Copyright © 2015年 zhk. All rights reserved.
//

#import "EditViewController.h"
#import "EditScrollView.h"
#import "DataSqlite.h"
#import "IQAudioRecorderController.h"
@interface EditViewController ()<UIActionSheetDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate,UITextFieldDelegate,IQAudioRecorderControllerDelegate,UINavigationControllerDelegate>
@property(nonatomic,strong)EditScrollView * edVC;
@property(nonatomic,assign)BOOL first;
@property(nonatomic,assign)BOOL second;
@property(nonatomic,assign)BOOL third;
@property(nonatomic,assign)BOOL luyin;

@property(nonatomic,strong)NSString * passString;
@end

@implementation EditViewController

-(void)loadView
{
    self.first = NO;
    self.second = NO;
    self.third = NO;
    self.luyin = NO;
    
    self.edVC = [[EditScrollView alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
    self.view = self.edVC;
    self.edVC.contentSize = CGSizeMake(self.view.frame.size.width, 650);
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"保存" style:(UIBarButtonItemStyleDone) target:self action:@selector(rightAction)];
    
    [self.edVC.button addTarget:self action:@selector(buttonAction) forControlEvents:(UIControlEventTouchUpInside)];
    self.edVC.textField.delegate = self;
    self.edVC.textField.returnKeyType = UIReturnKeyDone;
    
    
}


- (void)buttonAction
{
    UIActionSheet * sheet = [[UIActionSheet alloc]initWithTitle:@"最多添加三张照骗" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"拍照" otherButtonTitles:@"相册",@"录音", nil];
    
    
    [sheet showInView:self.edVC];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    
    NSInteger sourceType = 0;
    if (buttonIndex == 0) {
        if (self.third == YES) {
            UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"最多只能添加三张照片" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
            [alert show];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [alert dismissWithClickedButtonIndex:0 animated:YES];
            });
        }
        else
        {
            UIImagePickerController * imagePicker = [[UIImagePickerController alloc]init];
            imagePicker.delegate = self;
            imagePicker.allowsEditing = YES;
            sourceType = UIImagePickerControllerSourceTypeCamera;
            imagePicker.sourceType = sourceType;
            [self presentViewController:imagePicker animated:YES completion:nil];
        }
    }else if (buttonIndex == 1)
    {
        if (self.third == YES) {
            UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"最多只能添加三张照片" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
            [alert show];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [alert dismissWithClickedButtonIndex:0 animated:YES];
            });
        }else
        {
            UIImagePickerController * imagePicker = [[UIImagePickerController alloc]init];
            imagePicker.delegate = self;
            imagePicker.allowsEditing = YES;
            [self presentViewController:imagePicker animated:YES completion:nil];
            imagePicker.sourceType = sourceType;
            sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        }
        
    } else if(buttonIndex == 2)
    {
        IQAudioRecorderController * controller = [[IQAudioRecorderController alloc]init];
        controller.delegate = self;
        if (self.luyin == NO) {
            [self presentViewController:controller animated:YES completion:nil];
        }else
        {
            UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"只能添加一条录音" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
            [alert show];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [alert dismissWithClickedButtonIndex:0 animated:YES];
            });
        }
        
    }else if(buttonIndex == 3)
    {
        return;
    }
    
}


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    
    if (self.first == NO) {
        [picker dismissViewControllerAnimated:YES completion:^{}];
        UIImage * image = [info objectForKey:UIImagePickerControllerEditedImage];
        [self.edVC.image setImage:image];
        self.first = YES;
    }else
    {
        if (self.second == NO) {
            [picker dismissViewControllerAnimated:YES completion:^{}];
            UIImage * image = [info objectForKey:UIImagePickerControllerEditedImage];
            [self.edVC.imageTwo setImage:image];
            self.second = YES;
        }else if(self.third == NO)
        {
            [picker dismissViewControllerAnimated:YES completion:^{}];
            UIImage * image = [info objectForKey:UIImagePickerControllerEditedImage];
            [self.edVC.imageThree setImage:image];
            self.third = YES;
        }else  if(self.third == YES)
        {
            UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"最多只能添加三张照片" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
            [alert show];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [alert dismissWithClickedButtonIndex:0 animated:YES];
            });
            
            //            self.edVC.button.userInteractionEnabled = NO;
            //            [self.edVC.button setImage:[UIImage imageNamed:@"noadd.png"] forState:UIControlStateNormal];
        }
        
        
    }
}

- (void)rightAction
{
    if (self.edVC.textField.text.length != 0 || self.edVC.textView.text.length != 0 ) {
        if ([[DataSqlite shareDataSqlite] findExistByTile:self.edVC.textField.text]) {
            NSDate * time = [NSDate date];
            NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
            [formatter setDateFormat:@"YYYY MM dd"];
            NSString * locationString = [formatter stringFromDate:time];
            if (self.AcceptPath == nil) {
                self.AcceptPath = @"";
            }
            [[DataSqlite shareDataSqlite] insertTitle:self.edVC.textField.text Content:self.edVC.textView.text image:self.edVC.image.image currentTime:locationString image:self.edVC.imageTwo.image image:self.edVC.imageThree.image filePath:self.AcceptPath];
            [self.navigationController popToRootViewControllerAnimated:YES];
        }else
        {
            UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"存在同名标题，换个题目试试？" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
            [alert show];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [alert dismissWithClickedButtonIndex:0 animated:YES];
            });
        }
        
    }else
    {
        UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"没有标题或者内容我无法为您保存哦" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
        [alert show];
    }
    
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

- (void)audioRecorderController:(IQAudioRecorderController *)controller didFinishWithAudioAtPath:(NSString *)filePath
{
    self.luyin = YES;
    self.passString = filePath;
    NSArray * temp = [filePath componentsSeparatedByString:@"Sound/"];
    
    self.AcceptPath = temp[1];
    
    self.edVC.luyinLabel.hidden = NO;
}

- (void)audioRecorderControllerDidCancel:(IQAudioRecorderController *)controller
{
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
